﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CarolLib;
using DispatchAssist.BusinessLogical.Model;

namespace DispatchAssist.Model
{
    public class SMSAlertAllInfo
    {

        /// <summary>
        /// 
        /// </summary>
        public Guid SMSAlertID { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public Guid MeteoMsgID { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public Guid SeatID { get; set; }

        /// <summary>
        /// 航空公司代码
        /// </summary>
        public string GroupCode { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string RelationFlights { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int FlightCount { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int AlertActionFlag { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int PerformActionFlag { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string SeatName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string MAC { get; set; }

        /// <summary>
        /// 预警机场
        /// </summary>
        public string Airport { get; set; }

        /// <summary>
        /// 预警时间
        /// </summary>
        public DateTime WarnTime { get; set; }

        /// <summary>
        /// 预警级别
        /// </summary>
        public int WarnLevel { get; set; }

        /// <summary>
        /// 预警原始报文
        /// </summary>
        public string WarnMsg { get; set; }

        /// <summary>
        /// 中文气象预警信息
        /// </summary>
        public string WarnInfo { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string WarnStandard { get; set; }

        public string AirportFullName { get; set; }

        /// <summary>
        /// 等级名称
        /// </summary>
        public string WarnLevelName
        {
            get
            {
                switch (WarnLevel)
                {
                    case 1:
                        return "红色预警";
                    case 2:
                        return "黄色预警";
                    case 3:
                        return "蓝色预警";
                    default:
                        return "";
                }

            }
        }

        public string MessageBody { get; set; }

        public string MsgTemplateContent { get; set; }

        public DateTime? AlertCreateTime { get; set; }

        public string FlightInfoList
        {
            get
            {
                string result = "";

                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

                try
                {
                    doc.LoadXml(this.RelationFlights);

                    System.Xml.XmlNodeList flights = doc.SelectNodes("//flight");

                    foreach (System.Xml.XmlNode flight in flights)
                    {
                        result += flight.Attributes["carrier"].Value.ToString() + flight.Attributes["flightNo"].Value.ToString() + ",";
                    }

                }
                catch (Exception e)
                {
                    Log.Exception(e);
                }

                return result.TrimEnd(',');
            }
        }

        public List<int> SplitActionFlags()
        {
            List<int> flags = new List<int>();

            if (((AlertActionFlag | (int)Enums.AlertActionFlag.ManualSendSMSAlert) == AlertActionFlag) && ((PerformActionFlag & (int)Enums.AlertActionFlag.ManualSendSMSAlert) == 0))
            {
                flags.Add((int)Enums.AlertActionFlag.ManualSendSMSAlert);
            }

            if (((AlertActionFlag | (int)(Enums.AlertActionFlag.AutoSendSMSAlert | Enums.AlertActionFlag.ConfirmSMS)) == AlertActionFlag) && ((PerformActionFlag & (int)Enums.AlertActionFlag.ConfirmSMS) == 0))
            {
                flags.Add((int)(Enums.AlertActionFlag.AutoSendSMSAlert | Enums.AlertActionFlag.ConfirmSMS));
            }


            return flags;
        }

        public SMSAlertAllInfo CopyModel()
        {
            SMSAlertAllInfo info = new SMSAlertAllInfo();

            info.Airport = this.Airport;
            info.AirportFullName = this.AirportFullName;
            info.AlertActionFlag = this.AlertActionFlag;
            info.AlertCreateTime = this.AlertCreateTime;
            info.FlightCount = this.FlightCount;
            info.GroupCode = this.GroupCode;
            info.MessageBody = this.MessageBody;
            info.MeteoMsgID = this.MeteoMsgID;
            info.MsgTemplateContent = this.MsgTemplateContent;
            info.PerformActionFlag = this.PerformActionFlag;
            info.RelationFlights = this.RelationFlights;
            info.SeatID = this.SeatID;
            info.SeatName = this.SeatName;
            info.SMSAlertID = this.SMSAlertID;
            info.WarnInfo = this.WarnInfo;
            info.WarnLevel = this.WarnLevel;
            info.WarnMsg = this.WarnMsg;
            info.WarnStandard = this.WarnStandard;
            info.WarnTime = this.WarnTime;

            return info;
        }

        public List<SMSAlertAllInfo> SplitModel()
        {
            List<int> actions = this.SplitActionFlags();

            List<SMSAlertAllInfo> result = new List<SMSAlertAllInfo>();
            foreach (int action in actions)
            {

                SMSAlertAllInfo info = this.CopyModel();
                info.AlertActionFlag = action;

                result.Add(info);
            }

            return result;
        }

    }
}
